<template>
    <a-form ref='formRef' :label-col='{span : 5}' :model='formData'>
        <a-form-item style='display:none' name='id'>
            <a-input type='hidden' v-model:value='formData.id' />
        </a-form-item>
        <a-form-item label='编码' name='code' :rules="[{required : true, message : '请填写编码'}]">
            <a-input v-model:value='formData.code' />
        </a-form-item>
        <template v-for="(item, index) in i18n.locales">
            <a-form-item :label='item.name' :name="['texts', item.code]">
                <a-input v-model:value='formData.texts[item.code]' />
            </a-form-item>
        </template>
    </a-form>
</template>

<script lang="ts" setup>
        import { saveApi } from '@/api/system/Lang'
        import { ref } from 'vue';
import { useI18nStore } from '@/store/i18n';

    const i18n = useI18nStore()

    const props = defineProps<{
        row? : any;
    }>()

    const formRef = ref()

    const coverList = ref<any[]>([])

    
    const formData = ref<{id:number, code:string, texts:{[key:string] : string}}>({
        id : 0,
        code : '',
        texts : {}
    })

    i18n.locales.forEach((item, index) => {
        formData.value.texts[item.code] = ''
        
    })
    

    if(props.row) {
        for(let k in props.row) {
            if(k in formData.value) {
                if(k == 'texts') {
                    for(let kk in formData.value.texts) {
                        formData.value.texts[kk] = props.row[k][kk] || ''
                    }
                }else {
                    formData.value[k] = props.row[k]
                }
            }
        }
    }else {
        
    }




    async function submit() {
        let params  = await formRef.value.validate()
        console.log(params)
        return saveApi(params)
    }

    const parentOptions = ref<any[]>([{id : 0, name : '顶级分类', children : []}])
        const chains = ref<any[]>([])
    
    defineExpose({
        submit
    })
</script>